//
//  GHManyPic.m
//  VideoPic
//
//  Created by GH on 16/3/30.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "GHManyPic.h"
#import "GHPicGroupModel.h"
#import <YYWebImage.h>
#import "HomeViewController.h"
#import "GHPushLargeVC.h"




#define CELL_WIDTH self.bounds.size.width


@interface GHManyPic ()<UIGestureRecognizerDelegate,UICollectionViewDataSource ,UICollectionViewDelegate>
@property (nonatomic ,retain)UILabel *labelOfUserName;
@property (nonatomic ,retain)UILabel *labelContect;
@property (nonatomic ,retain)UILabel * labelOfCap;
@property (nonatomic ,retain)UIImageView *imageOfHead;
@property (nonatomic ,retain)GHPicGroupModel *model;
@property (nonatomic ,strong)NSMutableArray *arrWidth;
@property (nonatomic ,strong)NSMutableArray *arrHeight;
@property (nonatomic ,strong)UICollectionView *conllection;
@property (nonatomic ,strong)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic ,strong)UIImageView *image;
@property (nonatomic ,strong)NSMutableArray *arrX;
@property (nonatomic ,strong)NSMutableArray *arrY;
@property (nonatomic ,strong)NSMutableArray *arrFramex;
@property (nonatomic ,strong)NSMutableArray *arrFramey;
@property (nonatomic ,retain)UIButton *buttonWithDZ;
@property (nonatomic ,retain)UIButton *buttonWithdigg;
@property (nonatomic ,retain)UIButton *buttonComm;
@property (nonatomic ,retain)UIButton *buttonShare;
@property (nonatomic ,retain)UILabel *labelWithDZ;
@property (nonatomic ,retain)UILabel *labelWithdigg;
@property (nonatomic ,retain)UILabel *labelOfComm;
@property (nonatomic ,retain)UILabel *labelOfshare;




@end

@implementation GHManyPic


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSLog(@"重用");
        [self createImage];
        [self createLabel];
        [self createCollection];
        [self clickButton];
    }
    
    return self;
}


- (void)clickButton {
    
    self.buttonWithDZ = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonWithDZ];
    
    self.buttonWithdigg = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonWithdigg];
    
    self.buttonComm = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonComm];
    
    self.buttonShare = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonShare];
    
}




- (void)createCollection {
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.itemSize = CGSizeMake((CELL_WIDTH - 30) /3, (CELL_WIDTH - 30) /3);
    
    self.conllection = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:self.flowLayout];
    [self.contentView addSubview:self.conllection];
    self.conllection.backgroundColor = [UIColor whiteColor];
    
    self.conllection.delegate = self;
    self.conllection.dataSource = self;
    [self.conllection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return  self.model.arrPic.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    self.image = [[YYAnimatedImageView alloc] initWithFrame:CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
    self.image.backgroundColor = [UIColor whiteColor];
    [cell.contentView addSubview:self.image];
    self.image.tag = indexPath.row + 10;
    self.image.yy_imageURL = [NSURL URLWithString:self.model.arrPic[indexPath.row]];
    
    cell.backgroundColor = [UIColor redColor];
    
    
    [self.arrHeight addObject:self.model.height_large];
    [self.arrWidth addObject:self.model.width_large];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    id result = [self nextResponder];
    while (![result isKindOfClass:[HomeViewController class]] && result != nil) {
        result = [result nextResponder];
    }
    
    HomeViewController *vc =  (HomeViewController *)result;
    GHPushLargeVC *push = [[GHPushLargeVC alloc] init];
    
    
    vc.hidesBottomBarWhenPushed = YES;
    vc.navigationController.navigationBar.hidden = YES;
    [[NSNotificationCenter defaultCenter] addObserverForName:@"tabBarhiddenmany" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        vc.hidesBottomBarWhenPushed = NO;
    }];
    
    self.arrFramex = [NSMutableArray array];
    self.arrFramey = [NSMutableArray array];
    
    
    for (int i = 0;i <self.model.arrPic.count; i++) {
        
        UIImageView * image = [self.contentView viewWithTag:i + 10];
        
        CGRect frame = [image convertRect:[UIScreen mainScreen].bounds toView:0];
        CGPoint size = frame.origin;
        //        CGPoint y = frame.origin.y;
        [self.arrFramex addObject:NSStringFromCGPoint(size)];
        //        NSLog(@"%@",self.arrFrame);
    }
    //    CGPoint
    NSString *strsize = self.arrFramex[indexPath.row] ;
    CGPoint size = CGPointFromString(strsize);
    
    push.x = size.x;
    push.y = size.y;
    push.url = self.model.picUrl;
    push.height = self.model.r_height.integerValue;
    push.heightNew = self.model.r_height.integerValue;
    push.arrUrl = self.model.arrPic;
    
    push.arrHeight = self.arrHeight;
    push.arrWidth  = self.arrWidth;
    push.index = indexPath.row;
    push.arrFrame = self.arrFramex;
    [vc.navigationController pushViewController:push animated:NO];
    
}





- (void)createImage {
    
    
    self.imageOfHead = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imageOfHead];
    
}

- (void)createLabel {
    
    self.labelOfUserName = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfUserName];
    
    self.labelContect = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelContect];
    self.labelContect.numberOfLines = 0;
    
    self.labelOfCap = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfCap];
    self.labelOfCap.font = [UIFont systemFontOfSize:12];
    
}
- (void)passModelWith:(GHPicGroupModel *)model{
    
    self.model = model;
    self.model.r_height = @([UIScreen mainScreen].bounds.size.width *self.model.r_height.floatValue / self.model.r_width.floatValue);
    
    self.model = model;
    self.labelOfUserName.text = model.name;
    self.imageOfHead.yy_imageURL = [NSURL URLWithString:model.avatar_url];
    self.labelContect.text = model.content;
    self.labelContect.numberOfLines = 0;
    self.labelOfCap.text = model.category_name;
    self.arrHeight = [NSMutableArray array];
    self.arrWidth = [NSMutableArray array];
    self.arrX = [NSMutableArray array];
    self.arrY = [NSMutableArray array];
    
}




-(CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(CELL_WIDTH - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}



- (void)layoutSubviews {
    [super layoutSubviews];
    NSLog(@"%@",self.model.media_type);
    
    //头像
    self.imageOfHead.frame = CGRectMake(20, 20, CELL_WIDTH / 10, CELL_WIDTH / 10);
    self.imageOfHead.layer.masksToBounds = YES;
    self.imageOfHead.layer.cornerRadius = CELL_WIDTH / 20;
    
    //用户名
    self.labelOfUserName.frame = CGRectMake(30 + CELL_WIDTH / 10, 20 + CELL_WIDTH/ 20 - 13, CELL_WIDTH/2, 25);
    self.labelOfUserName.font = [UIFont systemFontOfSize:13];
    
    //title
    self.labelContect.frame = CGRectMake(20, 20 + CELL_WIDTH /10 +15, CELL_WIDTH  - 40, [self heightForLabel:self.labelContect.text]);
    CGFloat titleBom = [self heightForLabel:self.labelContect.text] + 35 + CELL_WIDTH / 10;
    
    CGFloat userHeight;
    
    //cap
    self.labelOfCap.numberOfLines = 0;
    if (self.labelContect == nil) {
        self.labelOfCap.frame = CGRectMake(20, titleBom + 15 - 20, [self heightForLabel:self.labelOfCap.text], 20);
        userHeight = titleBom + 25;
    }else {
        self.labelOfCap.frame = CGRectMake(20, titleBom + 15, 100, 20);
        userHeight = titleBom + 40;
    }
    self.labelOfCap.textAlignment = 1;
    self.labelOfCap.layer.borderWidth = 1;
    self.labelOfCap.layer.borderColor = [UIColor blackColor].CGColor;
    self.labelOfCap.layer.masksToBounds = YES;
    self.labelOfCap.layer.cornerRadius = 10;
    
    
    
    CGFloat all = userHeight + CELL_WIDTH +20;
    CGFloat collectionWidth = CELL_WIDTH;
    
    if (self.model.arrPic.count < 3) {
        all = userHeight + CELL_WIDTH / 3 + 20;
        collectionWidth = CELL_WIDTH /3;
    }else if (self.model.arrPic.count < 6) {
        all = userHeight + CELL_WIDTH / 3 *2 + 20;
        collectionWidth = CELL_WIDTH / 3 *2;
        
    }
    self.conllection.frame = CGRectMake(0, userHeight, CELL_WIDTH, collectionWidth);
    
    
    
    
    self.buttonWithDZ.frame = CGRectMake(20, all , 20, 20);
    [self.buttonWithDZ setImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
    
    self.buttonWithdigg.frame = CGRectMake(CELL_WIDTH / 3 - 10, all , 20, 20);
    [self.buttonWithdigg setImage:[UIImage imageNamed:@"bad"] forState:UIControlStateNormal];
    
    
    //buttonCom
    self.buttonComm.frame = CGRectMake(CELL_WIDTH /3 *2 - 40, all , 20, 20);
    [self.buttonComm setImage:[UIImage imageNamed:@"pinglun"] forState:UIControlStateNormal];
    
    //分享
    self.buttonShare.frame = CGRectMake(CELL_WIDTH - 80, all , 20, 20);
    [self.buttonShare setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    
    
    
    
    
    
}



+ (CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}


+ (CGFloat)heightForCell:(GHPicGroupModel *)model {
    
    CGFloat height;
    
    CGFloat titleBom = [[self class] heightForLabel:model.content] + 35 + [UIScreen mainScreen].bounds.size.width / 10;
    
    if (model.content == nil) {
        
        height = titleBom + 25;
    }else {
        
        height = titleBom + 40;
    }
    
    CGFloat all = height + [UIScreen mainScreen].bounds.size.width +20;
    
    if (model.arrPic.count < 3) {
        all = height + [UIScreen mainScreen].bounds.size.width / 3 + 20;
    }else if (model.arrPic.count < 6) {
        all = height + [UIScreen mainScreen].bounds.size.width / 3 *2 + 20;
        
    }
    
    
    
    return all + 40;
}


- (void)prepareForReuse{
    [super prepareForReuse];
    
    [self.conllection reloadData];
    
}
//
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
